<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            clearable
            v-model="queryInfo.query"
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddpage">添加商品 </el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table style="width: 100%" border stripe :data="goodsList">
        <el-table-column type="index" label="#" width="50"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"> </el-table-column>
        <el-table-column label="商品价格（元）" prop="goods_price" width="95px">
        </el-table-column>
        <el-table-column label="重量" prop="goods_weight" width="70px">
        </el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="150px">
          <template v-slot="scope">
            {{ scope.row.add_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <!-- 编辑按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row)"
            >
            </el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleceGood(scope.row.goods_id)"
            >
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改商品对话框 -->
    <el-dialog title="修改商品" :visible.sync="editDialogVisible" width="50%">
      <el-form ref="editFormRef" :model="editForm" :rules="editFormRules">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editForm.goods_price" type="number"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="editForm.goods_weight" type="number"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="editForm.goods_number" type="number"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoods">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 查询参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品列表数据
      goodsList: [],
      // 返回商品的总条数
      total: 0,
      // 编辑商品对话框开关
      editDialogVisible: false,
      // 表单校验对象
      editFormRules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输入商品数量', trigger: 'blur' }
        ]
      },
      // 修改商品表单对象
      editForm: {
        goods_id: 0,
        goods_name: 0,
        goods_price: 0,
        goods_weight: 0,
        goods_number: 0
      }
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    show (data) {
      console.log(data)
    },
    // 获取商品列表数据
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      console.log(res)
      if (res.meta.status !== 200) { return this.$message.error('获取商品列表数据失败！') }
      console.log(res.data.goods)
      this.goodsList = res.data.goods
      this.total = res.data.total
      console.log(this.queryInfo)
      return this.$message.success('获取商品列表数据成功！')
    },
    // 点击添加商品 跳转至添加商品页面
    goAddpage () {
      this.$router.push('/goods/add')
    },
    // 监听pageSize改变
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 监听currentPage 改变
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    // 展示编辑商品对话框
    showEditDialog (row) {
      console.log(row)
      this.editForm.goods_id = row.goods_id
      this.editForm.goods_name = row.goods_name
      this.editForm.goods_number = row.goods_number
      this.editForm.goods_price = row.goods_price
      this.editForm.goods_weight = row.goods_weight
      console.log(this.editForm)
      this.editDialogVisible = true
    },
    // 编辑商品
    async editGoods () {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) { return this.$message.error('表单校验未通过，请填写必填项') }
        // 表单校验通过，发起网络请求
        const { data: res } = await this.$http.put('goods/' + this.editForm.goods_id, this.editForm)
        console.log(res)
        if (res.meta.status !== 201) { return this.$message.error(res.meta.msg) }
        this.$message.success('修改商品成功！')
        // 关闭对话框，刷新列表
        this.editDialogVisible = false
        this.getGoodsList()
      })
    },
    // 根据id删除商品
    async deleceGood (id) {
      // 弹框提示是否删除
      const confirmResult = await this.$confirm('此操作将删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
      ).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      // 发起删除请求
      const { data: res } = await this.$http.delete('goods/' + id)
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败')
      }
      this.getGoodsList()
      this.$message.success('删除成功')
    }
  }
}
</script>
